package com.idanchuang.component.redis.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.core.task.SimpleAsyncTaskExecutor;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author yjy
 * Created at 2022/2/9 7:35 下午
 */
public class AcgAsyncTaskExecutor extends SimpleAsyncTaskExecutor implements DisposableBean {

    private static final Logger logger = LoggerFactory.getLogger(AcgAsyncTaskExecutor.class);

    private final ThreadPoolExecutor executor;

    {
        executor = new ThreadPoolExecutor(1, 1,
                1, TimeUnit.MINUTES, new LinkedBlockingQueue<>(50000),
                (run) -> new Thread(run, "key-expire-executor"));
    }

    @Override
    protected void doExecute(Runnable task) {
        executor.execute(task);
    }

    @Override
    public void destroy() throws Exception {
        logger.info("AcgAsyncTaskExecutor destroy...");
        executor.shutdown();
    }

}
